Trò chơi bắn đạn đơn giản sử dụng Unity
13.469 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class PlayerBulletCtrl : MonoBehaviour {
5
6 public Vector2 velocity;
7
8 Rigidbody2D rb;
9
10 // Use this for initialization
11 void Start () {
12 rb = GetComponent<Rigidbody2D>();
13 }
14
15 // Update is called once per frame
16 void Update () {
17 rb.velocity = velocity;
18 }
19
20 private void OnCollisionEnter2D(Collision2D collision)
21 {
22 if (collision.gameObject.CompareTag("Enemy"))
23 {
24 GameCtrl.instance.BulletHitEnemy(collision.gameObject.transform);
25 Destroy(gameObject);
26 }
27 else if (collision.gameObject.CompareTag("Player"))
28 {
29 Destroy(gameObject);
30 }
31 }
32
33 }
2 using System.Collections;
3
4 public class PlayerBulletCtrl : MonoBehaviour {
5
6 public Vector2 velocity;
7
8 Rigidbody2D rb;
9
10 // Use this for initialization
11 void Start () {
12 rb = GetComponent<Rigidbody2D>();
13 }
14
15 // Update is called once per frame
16 void Update () {
17 rb.velocity = velocity;
18 }
19
20 private void OnCollisionEnter2D(Collision2D collision)
21 {
22 if (collision.gameObject.CompareTag("Enemy"))
23 {
24 GameCtrl.instance.BulletHitEnemy(collision.gameObject.transform);
25 Destroy(gameObject);
26 }
27 else if (collision.gameObject.CompareTag("Player"))
28 {
29 Destroy(gameObject);
30 }
31 }
32
33 }
Use this for initialization
Update is called once per frame